//
//  oldFamilyBandParentsViewController.m
//  laonianbangfamily
//
//  Created by 勇拓 李 on 18/3/26.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldFamilyBandParentsViewController.h"
#import "oldUnRegisterParentsView.h"
#import "oldRegisterParentsView.h"
#import "oldRegex.h"
#import "AppDelegate.h"
#import "oldFamilyFirstViewController.h"
#import "oldFamilyParentListTableViewController.h"

@interface oldFamilyBandParentsViewController ()<oldRegisterParentsViewDelegate, oldUnRegisterParentsViewDelegate, UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *textFiled_mobile;
@property (weak, nonatomic) IBOutlet UITextField *textFiled_cer;
@property (weak, nonatomic) IBOutlet UIButton *btn_check;
@property (weak, nonatomic) IBOutlet UIView *bottomView;
@property (nonatomic, strong)oldServer *parent;

@property (nonatomic, strong)oldUnRegisterParentsView *unregisterView;
@property (nonatomic, strong)oldRegisterParentsView *registerView;
@end

@implementation oldFamilyBandParentsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (oldRegisterParentsView *)registerView
{
    if (!_registerView) {
        _registerView  = [[[NSBundle mainBundle] loadNibNamed:@"oldRegisterParentsView" owner:self options:nil] objectAtIndex:0];
        _registerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 346);
        _registerView.parent = _parent;
        _registerView.delegate = self;
        _registerView.phoneNum = _textFiled_mobile.text;
    }
    
    return _registerView;
}

- (oldUnRegisterParentsView *)unregisterView
{
    if (!_unregisterView) {
        _unregisterView  = [[[NSBundle mainBundle] loadNibNamed:@"oldUnRegisterParentsView" owner:self options:nil] objectAtIndex:0];
        _unregisterView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 346);
        _unregisterView.delegate = self;
        _unregisterView.phoneNum = _textFiled_mobile.text;
    }
    
    return _unregisterView;
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"绑定家属";
    _btn_check.layer.cornerRadius = 6.f;
    [_btn_check addTarget:self action:@selector(btn_check_tapped) forControlEvents:UIControlEventTouchDown];
    _textFiled_mobile.delegate = self;
}

#pragma mark - initData
- (void)initData
{
    
}

#pragma mark - btn
- (void)btn_check_tapped
{
    if (![oldRegex isMobileNo:_textFiled_mobile.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    if (![oldRegex isValidateIDNumber:_textFiled_cer.text]) {
        [oldHub warningUser:@"请输入正确的身份证号码"];
        return;
    }
    
    WeakSelf
    
    [oldHub waiting:@"查询中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_CheckExist params:@{@"role":@"1", @"mobile":_textFiled_mobile.text, @"idNum":_textFiled_cer.text} success:^(id data) {
        [oldHub hideHUD];
        if ([data isKindOfClass:[NSNull class]]) {
            [weakSelf initUnRegisterView];
        }
        else
        {
            weakSelf.parent = [[oldServer alloc] initWithDictionary:data];
            [weakSelf initRegisterView];
        }
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

#pragma mark - registerView

- (void)initRegisterView
{
    [self removeSubviews];
    
    [_bottomView addSubview:self.registerView];
}

- (void)sendBind:(NSString *)sms relation:(NSString *)relation
{
    if (![oldRegex isMobileNo:_textFiled_mobile.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    if (![oldRegex isSMSCode:sms]) {
        [oldHub warningUser:@"请输入正确的验证码"];
        return;
    }
    
    if (![relation length]) {
        [oldHub warningUser:@"请输入亲属关系"];
        return;
    }
    
    WeakSelf
    
    [oldHub waiting:@"绑定中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_BindUserOld params:@{@"userOldUid":_parent.uid, @"code":sms, @"relation":relation} success:^(id data) {
        [oldHub hideHUD];
        [weakSelf gotoRoot];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

- (void)removeSubviews
{
    for (UIView *subview in _bottomView.subviews) {
        if ([subview isKindOfClass:[oldUnRegisterParentsView class]] || [subview isKindOfClass:[oldRegisterParentsView class]]) {
            [subview removeFromSuperview];
        }
    }
}

#pragma mark - unRegisterView

- (void)initUnRegisterView
{
    [self removeSubviews];
    [_bottomView addSubview:self.unregisterView];
}

- (void)sendBind:(NSString *)sms relation:(NSString *)relation name:(NSString *)name password:(NSString *)password
{
    if (![oldRegex isMobileNo:_textFiled_mobile.text]) {
        [oldHub warningUser:@"请输入正确的手机号码"];
        return;
    }
    
    if (![oldRegex isSMSCode:sms]) {
        [oldHub warningUser:@"请输入正确的验证码"];
        return;
    }
    
    if (![oldRegex isValidatePassword:password]) {
        [oldHub warningUser:@"请输入数字或者字母组合的密码"];
        return;
    }
    
    if (![oldRegex isValidateIDNumber:_textFiled_cer.text]) {
        [oldHub warningUser:@"请输入正确的身份证号码"];
        return;
    }
    
    if (![relation length]) {
        [oldHub warningUser:@"请输入亲属关系"];
        return;
    }
    
#ifdef DEBUG

#else
    if (![oldRegex isChineseName:name])
    {
        [oldHub warningUser:@"请输入正确的名字"];
        return;
    }
#endif
    
    WeakSelf
    
    NSString *newPassword = [oldRegex md5:password];
    
    [oldHub waiting:@"绑定中.."];
    
    [OLDNetWork httpRequestWithPostPut:URL_RegisterAndBind params:@{@"mobile": _textFiled_mobile.text, @"uname":name , @"code":sms, @"relation":relation, @"password":newPassword, @"idNum":_textFiled_cer.text} success:^(id data) {
        [oldHub hideHUD];
        [weakSelf gotoRoot];
    } error:^(NSString *error) {
        [oldHub warningUser:error];
    }];
}

-(void)textField1TextChange:(UITextField *)textField{
    if (textField == _textFiled_mobile) {
        _registerView.phoneNum = textField.text;
        _unregisterView.phoneNum = textField.text;
    }
}

#pragma mark - gotoRoot
- (void)gotoRoot
{
    if(_isComeFromRegister)
    {
        [AppDelegate gotoTab];
    }
    else
    {
        UIViewController *vc = self.navigationController.viewControllers[[self.navigationController.viewControllers count] - 2];
        if ([vc isKindOfClass:[oldFamilyFirstViewController class]]) {
            oldFamilyFirstViewController *oldVc = (oldFamilyFirstViewController *)vc;
            [oldVc needReload];
        }
        else if ([vc isKindOfClass:[oldFamilyParentListTableViewController class]])
        {
            oldFamilyParentListTableViewController *oldVc = (oldFamilyParentListTableViewController *)vc;
            [oldVc needReload];
        }
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

@end
